#include "precompiled.h"
#include "enchant.h"
using namespace weo::engine::item;
using namespace std;
Enchant* Enchant::s_None = NULL;
Enchant::Enchant(std::string name, unsigned int id, std::vector<Attribute> attrs, unsigned int slot)
{
    m_Name = name;
    m_Id = id;
    if(!attrs.empty())
    {
        m_Attributes.assign(attrs.begin(),attrs.end());
    }
    else m_Attributes.clear();
    m_Level = 70;
    m_Slot = slot;
}
const Enchant* Enchant::None()
{
    if(!Enchant::s_None)
    {
        s_None = new Enchant("None",99999);
    }
    return s_None;
}
const Enchant* Enchant::GetEnchant(const unsigned int slot, int nr)
{
    if(weo::engine::item::Enchants.empty()) BuildEnchantVector(false);
    if(weo::engine::item::Enchants[slot].size() == 1) return 0;
    if(nr == -1) nr = rand() % weo::engine::item::Enchants[slot].size();
    while(weo::engine::item::Enchants[slot][nr].Id() == 99999)
    {
        nr = rand() % weo::engine::item::Enchants[slot].size();
    }
    return &weo::engine::item::Enchants[slot][nr];
}
const std::vector<std::vector<Enchant>>* Enchant::GetEnchants()
{
    if(weo::engine::item::Enchants.empty()) BuildEnchantVector(false);
    return &weo::engine::item::Enchants;
}
const Enchant* Enchant::GetEnchantById(const unsigned int id)
{
    if(weo::engine::item::Enchants.empty()) BuildEnchantVector(false);
    std::vector<std::vector<Enchant>>::const_iterator it;
    std::vector<Enchant>::const_iterator it2;
    for(it = Enchants.begin();it != Enchants.end();it++)
    {
        for(it2 = it->begin();it2 != it->end();it2++)
        {
            if(it2->Id() == id)
            {
                const Enchant* enchant = &*it2;
                return enchant;
                break;
            }
        }
    }
    return NULL;
}
Enchant& Enchant::operator =(const weo::engine::item::Enchant &enchant)
{
    if(this != &enchant)
    {
        m_Name = enchant.Name();
        m_Id = enchant.Id();
        m_Slot = enchant.Slot();
        m_Attributes.clear();
        if(!enchant.Attributes()->empty())
        {
            for(unsigned int i = 0;i<enchant.Attributes()->size();i++)
            {
                m_Attributes.push_back(enchant.Attributes()->at(i));
            }
        }
    }
    return *this;
}
bool Enchant::operator==(const Enchant& other)
{
    return m_Id == other.Id();
}
bool Enchant::operator<(const Enchant& other)
{
    return m_Id < other.Id();
}
bool cmpptr(const Enchant &a, const Enchant &b)
{
    return a.Name() < b.Name();
}
void weo::engine::item::Enchant::BuildEnchantVector(bool useFinger)
{
    vector<Enchant> vector;
    weo::engine::item::EnchantSlot["None"] = 0;
    weo::engine::item::EnchantSlot["Head"] = 1;
    weo::engine::item::EnchantSlot["Shoulder"] = 2;
    weo::engine::item::EnchantSlot["Back"] = 4;
    weo::engine::item::EnchantSlot["Chest"] = 8;
    weo::engine::item::EnchantSlot["Wrist"] = 16;
    weo::engine::item::EnchantSlot["Hands"] = 32;
    weo::engine::item::EnchantSlot["Legs"] = 64;
    weo::engine::item::EnchantSlot["Feet"] = 128;
    weo::engine::item::EnchantSlot["Finger"] = 256;
    weo::engine::item::EnchantSlot["Onehand"] = 512;
    weo::engine::item::EnchantSlot["Twohand"] = 1024;
    weo::engine::item::EnchantSlot["Shield"] = 2048;
    // head start
    //
    std::vector<weo::engine::item::Attribute> attr;
    attr.push_back(Attribute("Healing", 35));
    attr.push_back(Attribute("MP5", 7));
    vector.push_back(Enchant("Glyph of Renewal", 3001, attr, EnchantSlot["Head"]));
    attr.clear();
    attr.push_back(Attribute("SpellDamage", 22));
    attr.push_back(Attribute("HitSpellRating", 14));
    vector.push_back(Enchant("Glyph of Power", 3002, attr, EnchantSlot["Head"]));
    attr.clear();
    attr.push_back(Attribute("AttackPower", 34));
    attr.push_back(Attribute("HitRating", 16));
    vector.push_back(Enchant("Glyph of Ferocity", 3003, attr, EnchantSlot["Head"]));
    attr.clear();
    attr.push_back(Attribute("Strength", 17));
    attr.push_back(Attribute("Intellect", 16));
    vector.push_back(Enchant("Glyph of the Outcast", 3096, attr, EnchantSlot["Head"]));
    attr.clear();
    attr.push_back(Attribute("Stamina", 10));
    attr.push_back(Attribute("DefenseRating", 10));
    attr.push_back(Attribute("BlockValue", 15));
    vector.push_back(Enchant("Presence of Might", 2583, attr, EnchantSlot["Head"]));
    attr.clear();
    attr.push_back(Attribute("DefenseRating", 16));
    attr.push_back(Attribute("DodgeRating", 17));
    vector.push_back(Enchant("Glyph of the Defender", 2999, attr, EnchantSlot["Head"]));
    // head end
    //
    // shoulder start
    //
    attr.clear();
    attr.push_back(Attribute("Healing", 33));
    attr.push_back(Attribute("MP5", 4));
    vector.push_back(Enchant("Greater Inscription of Faith", 2980, attr, EnchantSlot["Shoulder"]));
    attr.clear();
    attr.push_back(Attribute("SpellDamage", 18));
    attr.push_back(Attribute("CritSpellRating", 10));
    vector.push_back(Enchant("Greater Inscription of Discipline", 2982, attr, EnchantSlot["Shoulder"]));
    attr.clear();
    attr.push_back(Attribute("AttackPower", 30));
    attr.push_back(Attribute("CritRating", 10));
    vector.push_back(Enchant("Greater Inscription of Vengeance", 2986, attr, EnchantSlot["Shoulder"]));
    attr.clear();
    attr.push_back(Attribute("DefenseRating", 15));
    attr.push_back(Attribute("DodgeRating", 10));
    vector.push_back(Enchant("Greater Inscription of Warding", 2991, attr, EnchantSlot["Shoulder"]));
    // shoulder end
    //
    // back start
    //
    attr.clear();
    attr.push_back(Attribute("Armor", 120));
    vector.push_back(Enchant("Major Armor", 2662, attr, EnchantSlot["Back"]));
    attr.clear();
    attr.push_back(Attribute("Agility", 12));
    vector.push_back(Enchant("Major Agility", 368, attr, EnchantSlot["Back"]));
    // back end
    //
    // chest start
    attr.clear();
    attr.push_back(Attribute("MP5", 6));
    vector.push_back(Enchant("Restore Mana Prime", 3150, attr, EnchantSlot["Chest"]));
    attr.clear();
    attr.push_back(Attribute("Health", 150));
    vector.push_back(Enchant("Exceptional Health", 2659, attr, EnchantSlot["Chest"]));
    attr.clear();
    attr.push_back(Attribute("Mana", 150));
    vector.push_back(Enchant("Exceptional Mana", 1483, attr, EnchantSlot["Chest"]));
    attr.clear();            
    attr.push_back(Attribute("Intellect", 6));
    attr.push_back(Attribute("Stamina", 6));
    attr.push_back(Attribute("Strength", 6));
    attr.push_back(Attribute("Agility", 6));
    attr.push_back(Attribute("Spirit", 6));
    vector.push_back(Enchant("Exceptional Stats", 2661, attr, EnchantSlot["Chest"]));
    attr.clear();
    attr.push_back(Attribute("ResilienceRating", 15));
    vector.push_back(Enchant("Major Resilience", 2933, attr, EnchantSlot["Chest"]));
    // chest end
    //
    // bracer start
    attr.clear();
    attr.push_back(Attribute("SpellDamage", 15));
    vector.push_back(Enchant("Spellpower", 2650, attr, EnchantSlot["Wrist"]));
    attr.clear();
    attr.push_back(Attribute("DefenseRating", 12));
    vector.push_back(Enchant("Major Defense", 1957, attr, EnchantSlot["Wrist"]));
    attr.clear();
    attr.push_back(Attribute("MP5", 6));
    vector.push_back(Enchant("Restore Mana Prime", 2679, attr, EnchantSlot["Wrist"]));
    attr.clear();
    attr.push_back(Attribute("Healing", 30));
    vector.push_back(Enchant("Superior Healing", 2617, attr, EnchantSlot["Wrist"]));
    attr.clear();
    attr.push_back(Attribute("AttackPower", 24));
    vector.push_back(Enchant("Assault", 1593, attr, EnchantSlot["Wrist"]));
    attr.clear();
    attr.push_back(Attribute("Stamina", 12));
    vector.push_back(Enchant("Fortitude", 2649, attr, EnchantSlot["Wrist"]));
    attr.clear();
    attr.push_back(Attribute("Intellect", 12));
    vector.push_back(Enchant("Major Intellect", 2749, attr, EnchantSlot["Wrist"]));
    attr.clear();
    attr.push_back(Attribute("Strength", 12));
    vector.push_back(Enchant("Brawn", 2647, attr, EnchantSlot["Wrist"]));
    attr.clear();
    attr.push_back(Attribute("Intellect", 4));
    attr.push_back(Attribute("Stamina", 4));
    attr.push_back(Attribute("Strength", 4));
    attr.push_back(Attribute("Agility", 4));
    attr.push_back(Attribute("Spirit", 4));
    vector.push_back(Enchant("Exceptional Stats", 1891, attr, EnchantSlot["Wrist"]));
    // bracer end
    // hands
    attr.clear();
    attr.push_back(Attribute("SpellDamage", 20));
    vector.push_back(Enchant("Major Spellpower", 2937, attr, EnchantSlot["Hands"]));
    attr.clear();
    attr.push_back(Attribute("HitSpellRating", 15));
    vector.push_back(Enchant("Spell Strike", 2935, attr, EnchantSlot["Hands"]));
    attr.clear();
    attr.push_back(Attribute("Healing", 35));
    vector.push_back(Enchant("Major Healing", 2322, attr, EnchantSlot["Hands"]));
    attr.clear();
    attr.push_back(Attribute("AttackPower", 26));
    vector.push_back(Enchant("Assault", 2983, attr, EnchantSlot["Hands"]));
    attr.clear();
    attr.push_back(Attribute("CritSpellRating", 10));
    vector.push_back(Enchant("Blasting", 2934, attr, EnchantSlot["Hands"]));
    attr.clear();
    attr.push_back(Attribute("Strength", 15));
    vector.push_back(Enchant("Major Strength", 684, attr, EnchantSlot["Hands"]));
    attr.clear();
    attr.push_back(Attribute("Agility", 15));
    vector.push_back(Enchant("Major Agility", 2564, attr, EnchantSlot["Hands"]));
    // hands end
    // legs start
    attr.clear();
    attr.push_back(Attribute("SpellDamage", 35));
    attr.push_back(Attribute("Stamina", 20));
    vector.push_back(Enchant("Runic Spellthread", 2748, attr, EnchantSlot["Legs"]));
    attr.clear();
    attr.push_back(Attribute("Healing", 66));
    attr.push_back(Attribute("Stamina", 20));
    vector.push_back(Enchant("Golden Spellthread", 2746, attr, EnchantSlot["Legs"]));
    attr.clear();
    attr.push_back(Attribute("Agility", 12));
    attr.push_back(Attribute("Stamina", 40));
    vector.push_back(Enchant("Nethercleft Leg Armor", 3013, attr, EnchantSlot["Legs"]));
    attr.clear();
    attr.push_back(Attribute("AttackPower", 50));
    attr.push_back(Attribute("CritRating", 12));
    vector.push_back(Enchant("Nethercobra Leg Armor", 3012, attr, EnchantSlot["Legs"]));
    // legs end
    // feet start
    attr.clear();
    attr.push_back(Attribute("HitRating", 10));
    vector.push_back(Enchant("Surefooted", 2658, attr, EnchantSlot["Feet"]));
    attr.clear();
    attr.push_back(Attribute("Agility", 6));
    vector.push_back(Enchant("Cat's Swiftness", -1, attr, EnchantSlot["Feet"]));
    attr.clear();
    attr.push_back(Attribute("Stamina", 9));
    vector.push_back(Enchant("Boar's Speed", 2940, attr, EnchantSlot["Feet"]));
    attr.clear();
    attr.push_back(Attribute("HP5", 4));
    attr.push_back(Attribute("MP5", 4));
    vector.push_back(Enchant("Vitality", 2656, attr, EnchantSlot["Feet"]));
    attr.clear();
    attr.push_back(Attribute("Agility", 12));
    vector.push_back(Enchant("Dexterity", 2657, attr, EnchantSlot["Feet"]));
    attr.clear();
    attr.push_back(Attribute("Stamina", 12));
    vector.push_back(Enchant("Fortitude", 2649, attr, EnchantSlot["Feet"]));
    // feet stop
    // finger start
    attr.clear();
    attr.push_back(Attribute("Special", 99999));
    vector.push_back(Enchant("Weapon Might", 2929, attr, EnchantSlot["Finger"]));
    attr.clear();
    attr.push_back(Attribute("Intellect", 4));
    attr.push_back(Attribute("Stamina", 4));
    attr.push_back(Attribute("Strength", 4));
    attr.push_back(Attribute("Agility", 4));
    attr.push_back(Attribute("Spirit", 4));
    vector.push_back(Enchant("Stats", 2931, attr, EnchantSlot["Finger"]));
    attr.clear();
    attr.push_back(Attribute("Healing", 20));
    vector.push_back(Enchant("Healing Power", 2930, attr, EnchantSlot["Finger"]));
    attr.clear();
    attr.push_back(Attribute("SpellDamage", 20));
    vector.push_back(Enchant("Spell Power", 2937, attr, EnchantSlot["Finger"]));
    // finger stop
    // shield start
    attr.clear();
    attr.push_back(Attribute("Stamina", 18));
    vector.push_back(Enchant("Major Stamina", 1071, attr, EnchantSlot["Shield"]));
    attr.clear();
    attr.push_back(Attribute("Intellect", 12));
    vector.push_back(Enchant("Intellect", 2654, attr, EnchantSlot["Shield"]));
    attr.clear();
    attr.push_back(Attribute("BlockRating", 15));
    vector.push_back(Enchant("Shield Block", 1985, attr, EnchantSlot["Shield"]));
    // shield end
    // weapons start
    attr.clear();
    attr.push_back(Attribute("Special", 1000000));
    vector.push_back(Enchant("Major Striking", 963, attr, EnchantSlot["Onehand"] | EnchantSlot["Twohand"]));
    attr.clear();
    attr.push_back(Attribute("Special", 1000010));
    attr.push_back(Attribute("Special", 1000011));
    vector.push_back(Enchant("Mongoose", 2673, attr, EnchantSlot["Onehand"] | EnchantSlot["Twohand"]));
    attr.clear();
    attr.push_back(Attribute("Special", 1000020));
    vector.push_back(Enchant("Crusader", 1900, attr, EnchantSlot["Onehand"] | EnchantSlot["Twohand"]));
    attr.clear();
    attr.push_back(Attribute("Healing", 85));
    vector.push_back(Enchant("Major Healing", 2343, attr, EnchantSlot["Onehand"] | EnchantSlot["Twohand"]));
    attr.clear();
    attr.push_back(Attribute("SpellDamage", 40));
    vector.push_back(Enchant("Major Spellpower", 2669, attr, EnchantSlot["Onehand"] | EnchantSlot["Twohand"]));
    attr.clear();
    attr.push_back(Attribute("FireSpellDamage", 50));
    attr.push_back(Attribute("ArcaneSpellDamage", 50));
    vector.push_back(Enchant("Sunfire", 2671, attr, EnchantSlot["Onehand"] | EnchantSlot["Twohand"]));
    attr.clear();
    attr.push_back(Attribute("ShadowSpellDamage", 54));
    attr.push_back(Attribute("FrostSpellDamage", 54));
    vector.push_back(Enchant("Soulfrost", 2672, attr, EnchantSlot["Onehand"] | EnchantSlot["Twohand"]));
    attr.clear();
    attr.push_back(Attribute("Intellect", 30));
    vector.push_back(Enchant("Major Intellect", 1133, attr, EnchantSlot["Onehand"] | EnchantSlot["Twohand"]));
    attr.clear();
    attr.push_back(Attribute("Strength", 20));
    vector.push_back(Enchant("Potency", 1047, attr, EnchantSlot["Onehand"] | EnchantSlot["Twohand"]));
    attr.clear();
    attr.push_back(Attribute("Agility", 15));
    vector.push_back(Enchant("Agility", 883, attr, EnchantSlot["Onehand"] | EnchantSlot["Twohand"]));
    attr.clear();
    attr.push_back(Attribute("Spirit", 20));
    vector.push_back(Enchant("Mighty Spirit", 1149, attr, EnchantSlot["Onehand"] | EnchantSlot["Twohand"]));
    // weapons end
    // twohand start
    attr.clear();
    attr.push_back(Attribute("Special", 1000030));
    vector.push_back(Enchant("Superior Impact", 1896, attr, EnchantSlot["Twohand"]));
    attr.clear();
    attr.push_back(Attribute("Agility", 35));
    vector.push_back(Enchant("Major Agility", 1112, attr, EnchantSlot["Twohand"]));
    attr.clear();
    attr.push_back(Attribute("AttackPower", 70));
    vector.push_back(Enchant("Savagery", 1616, attr, EnchantSlot["Twohand"]));
    // twohand stop
    // special
    attr.clear();
    attr.push_back(Attribute("Stamina", 8));
    vector.push_back(Enchant("Knothide Armor Set", 2792, attr, EnchantSlot["Chest"] | EnchantSlot["Hands"] | EnchantSlot["Legs"] | EnchantSlot["Feet"]));
    attr.push_back(Attribute("Stamina", 10));
    vector.push_back(Enchant("Heavy Knothide Armor Set", 2841, attr, EnchantSlot["Head"] | EnchantSlot["Shoulder"] |  EnchantSlot["Chest"] | EnchantSlot["Hands"] | EnchantSlot["Legs"] | EnchantSlot["Feet"]));
    for(int i = 0;i<20;i++)
    {
        std::vector<Enchant> vec;
        vec.push_back(*Enchant::None());
        Enchants.push_back(vec);
    }
    for(unsigned int i = 0;i<vector.size();i++)
    {
        for(int k = 0;k<13;k++)
        {
            //{ None = 0, Head = 1, Shoulder = 2, Back = 4, Chest = 8, Wrist = 16, Hands = 32, Legs = 64, Feet = 128, Finger = 256, OneHand = 512, TwoHand = 1024, Shield = 2048 };
            // slots head,neck,shoulder,cloak,chest,wrist,hands,waist,legs,feet, ring, ring, trinket, trinket, weapon, weapon, ranged 
            unsigned int value = vector[i].Slot() & (int) pow(2.0f,k);
            switch(value)
            {
            case 1:
                Enchants[0].push_back(vector[i]);
                break;
            case 2:
                Enchants[2].push_back(vector[i]);
                break;
            case 4:
                Enchants[3].push_back(vector[i]);
                break;
            case 8:
                Enchants[4].push_back(vector[i]);
                break;
            case 16:
                Enchants[5].push_back(vector[i]);
                break;
            case 32:
                Enchants[6].push_back(vector[i]);
                break;
            case 64:
                Enchants[8].push_back(vector[i]);
                break;
            case 128:
                Enchants[9].push_back(vector[i]);
                break;
            case 256:
                if(!useFinger) break;
                Enchants[10].push_back(vector[i]);
                Enchants[11].push_back(vector[i]);
                break;
            case 512:
                Enchants[14].push_back(vector[i]);
                Enchants[15].push_back(vector[i]);
                break;
            case 1024:
                Enchants[17].push_back(vector[i]);
                break;
            case 2048:
                Enchants[18].push_back(vector[i]);
                break;
            default:
                break;
            }
        }
    }
    std::vector<std::vector<Enchant>>::iterator it;
    for(it = Enchants.begin();it!= Enchants.end();it++)
    {
        std::sort(it->begin(),it->end(),cmpptr);
    }
}